package com.pwc.conversionTool.entity;

import static com.pwc.common.Util.Time.getDateTime;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.Transient;

import org.codehaus.jackson.annotate.JsonIgnore;

import com.googlecode.objectify.Key;
import com.googlecode.objectify.annotation.Entity;
import com.googlecode.objectify.annotation.Id;
import com.googlecode.objectify.annotation.Ignore;
import com.googlecode.objectify.annotation.Index;
import com.pwc.conversionTool.dao.LogDao;


@Entity
public class TaskFlowLog implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 2882247044804952664L;
	
	
	@Id
	private Long id;
	
	private String logName;
	
	private List<String> messages;
	
	@Index protected Date dateTime;
	
	@Index private Key<TaskFlow> taskflowKey; 
	
	@Ignore
	private LogDao logDao=new LogDao();
	
	private boolean hasError = false;

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getLogName() {
		return logName;
	}

	public void setLogName(String logName) {
		this.logName = logName;
	}

	public List<String> getMessages() {
		return messages;
	}

	public void setMessages(List<String> messages) {
		this.messages = messages;
	}


	
    
    public void addMessage(String message)
    {
    	messages.add( com.pwc.common.Util.Time.getDateTime() +"  :  "+ message);
    	logDao.saveLog(this);
    }
    
    
    public void addMessage(List<String> messages)
    {
    	for (String msg : messages) {
    		addMessage(msg);
		}
    }
    
    
    public String getStringMessages()
    {
    	StringBuilder sb=new StringBuilder();
    	if(messages!=null)
    	{
    		for (String msg : messages) {
    			sb.append(msg).append("\n");
			}
    		
    		return sb.toString();
    	}else {
    		return null;
		}
    }
    

	public TaskFlowLog(String logName,Key<TaskFlow> taskflowKey) {
		super();
		this.logName = logName;
		this.messages =new ArrayList<String>() ;
		this.hasError = false;
		this.dateTime= new Date();
		this.taskflowKey=taskflowKey;
	}
    
	public TaskFlowLog() {
		super();
		this.logName ="";
		this.messages =new ArrayList<String>() ;
		this.hasError = false;
		this.dateTime= new Date();
	}

	

	public boolean isHasError() {
		return hasError;
	}

	public void setHasError(boolean hasError) {
		this.hasError = hasError;
	}

	@JsonIgnore
	public Key<TaskFlow> getTaskflowKey() {
		return taskflowKey;
	}

	public void setTaskflowKey(Key<TaskFlow> taskflowKey) {
		this.taskflowKey = taskflowKey;
	}
	
	public Date getDateTime() {
		return dateTime;
	}

	public void setDateTime(Date dateTime) {
		this.dateTime = dateTime;
	}

	
	@JsonIgnore
	@Transient
	public Key<TaskFlowLog> getKey() {
		return Key.create(TaskFlowLog.class, id);
	}
	

}
